package com.spremoteservice.android;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.client.ClientProtocolException;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class SiteContents extends Activity {

	private ListView listView1;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.site_contents);
	    
        Intent intent = getIntent();
        ListViewSPObject spObj = (ListViewSPObject)intent.getSerializableExtra("ListViewSPObjectParent");
        
        String objectType = spObj.objectType;

        CallAsyncSPGetListCollection listTask = null;
        CallAsyncSPGetFolderCollection folderTask = null;
        CallAsyncSPGetDocumentCollection documentTask = null;

        if (objectType.equals("RootSite")){
        	listTask = new CallAsyncSPGetListCollection();
            listTask.execute(new ListViewSPObject[]{spObj});
        } else if(objectType.equals("Library") || objectType.equals("Folder")){
        	folderTask = new CallAsyncSPGetFolderCollection();
        	folderTask.execute(new ListViewSPObject[]{spObj});

        	documentTask = new CallAsyncSPGetDocumentCollection();
        	documentTask.execute(new ListViewSPObject[]{spObj});
        }
        
    	try {
    		if (listTask != null){
    			listTask.get(120000, TimeUnit.MILLISECONDS);
    		}
    		if (folderTask != null){
    			folderTask.get(120000, TimeUnit.MILLISECONDS);
    		}
    		if (documentTask != null){
    			documentTask.get(120000, TimeUnit.MILLISECONDS);    			
    		}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    	List<ListViewSPObject> lists = null;
    	
        if (objectType.equals("RootSite")){
        	lists = listTask.GetResult();
        } else if(objectType.equals("Library")){
        	lists = folderTask.GetResult();
        	lists.addAll(documentTask.GetResult());
        }

        ListViewSPObjectAdapter adapter = new ListViewSPObjectAdapter(this, 
	                    R.layout.listview_item_row, lists);
	            
	            
        listView1 = (ListView)findViewById(R.id.listView1);
         
        View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
        listView1.addHeaderView(header);
        
        listView1.setAdapter(adapter);
        
        listView1.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position > 0)
                {
                	ListViewSPObjectHolder spObjHolder = (ListViewSPObjectHolder)view.getTag();
                	ListViewSPObject spObj = spObjHolder.spObject; 
                    if(spObj.objectType.equals("Document")){
                        Intent in = new Intent(SiteContents.this, ActivityDocument.class);
                        in.putExtra("ListViewSPObjectParent", spObj);
                    	startActivity(in);
                    }else if(spObj.objectType.equals("Library")){
                        Intent in = new Intent(SiteContents.this, SiteContents.class);
                        in.putExtra("ListViewSPObjectParent", spObj);
                    	startActivity(in);
                    }
                }
                
            }
        });
	}

}

class CallAsyncSPGetListCollection extends AsyncTask<ListViewSPObject, Void, List<ListViewSPObject>>{
	
	private List<ListViewSPObject> result;
	
	public List<ListViewSPObject> GetResult(){
		return result;
	}
	
	@Override
	protected List<ListViewSPObject> doInBackground(ListViewSPObject... arg0) {
		String siteUrl = arg0[0].siteUrl;
		List<ListViewSPObject> listCollection = null;
		try {
			listCollection = Connector.GetInstance().GetListCollection(siteUrl);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		result = listCollection;
		return listCollection;
	}
	
}

class CallAsyncSPGetFolderCollection extends AsyncTask<ListViewSPObject, Void, List<ListViewSPObject>>{
	
	private List<ListViewSPObject> result;
	
	public List<ListViewSPObject> GetResult(){
		return result;
	}
	
	@Override
	protected List<ListViewSPObject> doInBackground(ListViewSPObject... arg0) {
		String siteUrl = arg0[0].siteUrl;
		String libName = arg0[0].libraryName;
		String libId = arg0[0].libraryId;
		String libPath = arg0[0].folders;
		
		List<ListViewSPObject> listCollection = null;
		try {
			listCollection = Connector.GetInstance().GetFolderCollection(siteUrl, libName, libId, libPath);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		result = listCollection;
		return listCollection;
	}
	
}
class CallAsyncSPGetDocumentCollection extends AsyncTask<ListViewSPObject, Void, List<ListViewSPObject>>{
	
	private List<ListViewSPObject> result;
	
	public List<ListViewSPObject> GetResult(){
		return result;
	}
	
	@Override
	protected List<ListViewSPObject> doInBackground(ListViewSPObject... arg0) {
		String siteUrl = arg0[0].siteUrl;
		String libName = arg0[0].libraryName;
		String libId = arg0[0].libraryId;
		String libPath = arg0[0].folders;
		
		List<ListViewSPObject> documentCollection = null;
		try {
			documentCollection = Connector.GetInstance().GetDocumentCollection(siteUrl, libName, libId, libPath);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		result = documentCollection;
		return documentCollection;
	}
	
}
